package com.hpin.fansea.my;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.bean.ServiceDetail;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

/**
 * 服务详情
 * Created by Aaron on 15/10/23.
 */
public class ServiceDetailActivity extends BaseActivity {

    private TextView repair_address;
    private TextView tv_order_time;
    private TextView tv_show;
    private Button btn_status;
    private TextView tv_title;
    private String handleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_service_detail);

        initWidget();
/**
 *  mIntent.putExtra("handleId", serviceList.get(position).id_);
 mIntent.putExtra("handleName", serviceList.get(position).serviceName);
 */

        handleId = getIntent().getStringExtra("handleId");
        
        switch (getIntent().getStringExtra("handleName")) {

            case Constant.FW_XY:
                tv_title.setText("洗衣");
                break;
            case Constant.FW_JZ:
                tv_title.setText("家政");
                break;
            case Constant.FW_XC:
                tv_title.setText("洗车");
                break;
            case Constant.FW_KD:
                tv_title.setText("快递");
                break;
            case Constant.FW_FWZS:
                tv_title.setText("租售");
                break;
            case Constant.FW_PS:
                tv_title.setText("配送");
                break;

        }

        getServiceDetail();

    }


    /**
     * 服务详情
     */
    private void getServiceDetail() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.SERVICEDEAIL + "?data="
                + "&token=" + sp.getString(Constant.token, "") + "&handleId=" + handleId, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {


                try {

                    ServiceDetail serviceDetail = JSONObject.parseObject(str, ServiceDetail.class);

                    if (serviceDetail.status.equals(Constant.STATUS_SUCESS)) {

                        repair_address.setText(serviceDetail.data.address);
                        tv_order_time.setText(serviceDetail.data.serviceDate);
                        tv_show.setText(serviceDetail.data.remarks);


                        switch (serviceDetail.data.serviceStatus) {
                            case Constant.SERVICE_STATUS_WAIT:

                                btn_status.setText("取消预约");

                                btn_status.setClickable(true);
                                btn_status.setEnabled(true);
                                btn_status.setFocusable(true);

                                break;
                            case Constant.SERVICE_STATUS_ALREADY:

                                btn_status.setText("已受理");

                                btn_status.setClickable(false);
                                btn_status.setEnabled(false);
                                btn_status.setFocusable(false);

                                break;
                            case Constant.SERVICE_STATUS_FINSH:

                                btn_status.setText("已完成");

                                btn_status.setClickable(false);
                                btn_status.setEnabled(false);
                                btn_status.setFocusable(false);

                                break;
                            case Constant.SERVICE_STATUS_CANCEL:

                                btn_status.setText("已取消");

                                btn_status.setClickable(false);
                                btn_status.setEnabled(false);
                                btn_status.setFocusable(false);

                                break;

                        }

                    } else {
                        showToast(serviceDetail.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }

            }
        }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        iv_left.setOnClickListener(this);
        tv_title = (TextView) findViewById(R.id.tv_title);

        repair_address = (TextView) findViewById(R.id.repair_address);
        tv_order_time = (TextView) findViewById(R.id.tv_order_time);
        tv_show = (TextView) findViewById(R.id.tv_show);
        btn_status = (Button) findViewById(R.id.btn_status);

        btn_status.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

               case R.id.btn_status:

                   //取消预约
                   AlertDialog.Builder mbuilder = new AlertDialog.Builder(this);
                   mbuilder.setMessage("是否取消预约").setNegativeButton("保留预约", null).setPositiveButton("取消预约", new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int which) {

                           cancleOrder();

                       }
                   }).show();

                break;

        }
    }

    /**
     * 取消预约
     */
    private void cancleOrder() {

        HttpUtil.getInstance().setGetRequest("取消中...", this, Constant.WEB_ROOT + Constant.SERVICECANCLE + "?token="
                + sp.getString(Constant.token, "") + "&handleId=" + handleId, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (resultBaseBean.status.equals(Constant.STATUS_SUCESS)) {
                        showToast("取消成功");

                        btn_status.setText("已取消");

                        btn_status.setClickable(false);
                        btn_status.setEnabled(false);
                        btn_status.setFocusable(false);

                    }else {
                        showToast(resultBaseBean.message);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }

            }
        }, null);

    }
}